package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"web-service-demo/registry"
)

func main() {
	//启动心跳检测
	registry.SetupRegistryService()
	//设置HTTP handler
	http.Handle("/services", &registry.RegistryService{})

	ctx, cancel := context.WithCancel(context.Background())

	//设置HTTP服务器
	var srv http.Server
	srv.Addr = registry.ServerPost

	//后台启动HTTP服务器
	go func() {
		log.Println(srv.ListenAndServe())
		cancel()
	}()

	//后台接收关闭操作
	go func() {
		fmt.Println("Registry service started. Press any key to stop.")
		var s string
		fmt.Scanln(&s)
		srv.Shutdown(ctx) //关闭HTTP服务器
		cancel()
	}()

	//等待程序关闭
	<-ctx.Done()
	fmt.Println("Shutting down registry service.")
}
